<?php
define("EW_PAGE_ID", "view", TRUE); // Page ID
define("EW_TABLE_NAME", 'contact', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "contactinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
$Security->LoadCurrentUserLevel('contact');
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
if (!$Security->CanView()) {
	$Security->SaveLastUrl();
	Page_Terminate("contactlist.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$contact->Export = @$_GET["export"]; // Get export parameter
$sExport = $contact->Export; // Get export parameter, used in header
$sExportFile = $contact->TableVar; // Get export file, used in header
?>
<?php
if (@$_GET["c_id"] <> "") {
	$contact->c_id->setQueryStringValue($_GET["c_id"]);
} else {
	Page_Terminate("contactlist.php"); // Return to list page
}

// Get action
if (@$_POST["a_view"] <> "") {
	$contact->CurrentAction = $_POST["a_view"];
} else {
	$contact->CurrentAction = "I"; // Display form
}
switch ($contact->CurrentAction) {
	case "I": // Get a record to display
		if (!LoadRow()) { // Load record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "No records found"; // Set no record message
			Page_Terminate("contactlist.php"); // Return to list
		}
}

// Set return url
$contact->setReturnUrl("contactview.php");

// Render row
$contact->RowType = EW_ROWTYPE_VIEW;
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "view"; // Page id

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">View TABLE: contact
<br><br>
<a href="contactlist.php">Back to List</a>&nbsp;
<?php if ($Security->CanAdd()) { ?>
<a href="contactadd.php">Add</a>&nbsp;
<?php } ?>
<?php if ($Security->CanEdit()) { ?>
<a href="<?php echo $contact->EditUrl() ?>">Edit</a>&nbsp;
<?php } ?>
<?php if ($Security->CanAdd()) { ?>
<a href="<?php echo $contact->CopyUrl() ?>">Copy</a>&nbsp;
<?php } ?>
<?php if ($Security->CanDelete()) { ?>
<a href="<?php echo $contact->DeleteUrl() ?>">Delete</a>&nbsp;
<?php } ?>
</span>
</p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
	$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
}
?>
<p>
<form>
<table class="ewTable">
	<tr class="ewTableRow">
		<td class="ewTableHeader">Contact ID</td>
		<td<?php echo $contact->c_id->CellAttributes() ?>>
<div<?php echo $contact->c_id->ViewAttributes() ?>><?php echo $contact->c_id->ViewValue ?></div>
</td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Full Name</td>
		<td<?php echo $contact->c_fullname->CellAttributes() ?>>
<div<?php echo $contact->c_fullname->ViewAttributes() ?>><?php echo $contact->c_fullname->ViewValue ?></div>
</td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Email</td>
		<td<?php echo $contact->c_email->CellAttributes() ?>>
<div<?php echo $contact->c_email->ViewAttributes() ?>><?php echo $contact->c_email->ViewValue ?></div>
</td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Address</td>
		<td<?php echo $contact->c_address->CellAttributes() ?>>
<div<?php echo $contact->c_address->ViewAttributes() ?>><?php echo $contact->c_address->ViewValue ?></div>
</td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Telephone</td>
		<td<?php echo $contact->c_tel->CellAttributes() ?>>
<div<?php echo $contact->c_tel->ViewAttributes() ?>><?php echo $contact->c_tel->ViewValue ?></div>
</td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Subject</td>
		<td<?php echo $contact->c_subject->CellAttributes() ?>>
<div<?php echo $contact->c_subject->ViewAttributes() ?>><?php echo $contact->c_subject->ViewValue ?></div>
</td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Content</td>
		<td<?php echo $contact->c_content->CellAttributes() ?>>
<div<?php echo $contact->c_content->ViewAttributes() ?>><?php echo $contact->c_content->ViewValue ?></div>
</td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Date Add</td>
		<td<?php echo $contact->c_dateadd->CellAttributes() ?>>
<div<?php echo $contact->c_dateadd->ViewAttributes() ?>><?php echo $contact->c_dateadd->ViewValue ?></div>
</td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Status</td>
		<td<?php echo $contact->c_status->CellAttributes() ?>>
<div<?php echo $contact->c_status->ViewAttributes() ?>><?php echo $contact->c_status->ViewValue ?></div>
</td>
	</tr>
</table>
</form>
<p>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $contact;
	$sFilter = $contact->SqlKeyFilter();
	if (!is_numeric($contact->c_id->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@c_id@", ew_AdjustSql($contact->c_id->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$contact->Row_Selecting($sFilter);

	// Load sql based on filter
	$contact->CurrentFilter = $sFilter;
	$sSql = $contact->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$contact->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $contact;
	$contact->c_id->setDbValue($rs->fields('c_id'));
	$contact->c_fullname->setDbValue($rs->fields('c_fullname'));
	$contact->c_email->setDbValue($rs->fields('c_email'));
	$contact->c_address->setDbValue($rs->fields('c_address'));
	$contact->c_tel->setDbValue($rs->fields('c_tel'));
	$contact->c_subject->setDbValue($rs->fields('c_subject'));
	$contact->c_content->setDbValue($rs->fields('c_content'));
	$contact->c_dateadd->setDbValue($rs->fields('c_dateadd'));
	$contact->c_status->setDbValue($rs->fields('c_status'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $contact;

	// Call Row Rendering event
	$contact->Row_Rendering();

	// Common render codes for all row types
	// c_id

	$contact->c_id->CellCssStyle = "";
	$contact->c_id->CellCssClass = "";

	// c_fullname
	$contact->c_fullname->CellCssStyle = "";
	$contact->c_fullname->CellCssClass = "";

	// c_email
	$contact->c_email->CellCssStyle = "";
	$contact->c_email->CellCssClass = "";

	// c_address
	$contact->c_address->CellCssStyle = "";
	$contact->c_address->CellCssClass = "";

	// c_tel
	$contact->c_tel->CellCssStyle = "";
	$contact->c_tel->CellCssClass = "";

	// c_subject
	$contact->c_subject->CellCssStyle = "";
	$contact->c_subject->CellCssClass = "";

	// c_content
	$contact->c_content->CellCssStyle = "";
	$contact->c_content->CellCssClass = "";

	// c_dateadd
	$contact->c_dateadd->CellCssStyle = "";
	$contact->c_dateadd->CellCssClass = "";

	// c_status
	$contact->c_status->CellCssStyle = "";
	$contact->c_status->CellCssClass = "";
	if ($contact->RowType == EW_ROWTYPE_VIEW) { // View row

		// c_id
		$contact->c_id->ViewValue = $contact->c_id->CurrentValue;
		$contact->c_id->CssStyle = "";
		$contact->c_id->CssClass = "";
		$contact->c_id->ViewCustomAttributes = "";

		// c_fullname
		$contact->c_fullname->ViewValue = $contact->c_fullname->CurrentValue;
		$contact->c_fullname->CssStyle = "";
		$contact->c_fullname->CssClass = "";
		$contact->c_fullname->ViewCustomAttributes = "";

		// c_email
		$contact->c_email->ViewValue = $contact->c_email->CurrentValue;
		$contact->c_email->CssStyle = "";
		$contact->c_email->CssClass = "";
		$contact->c_email->ViewCustomAttributes = "";

		// c_address
		$contact->c_address->ViewValue = $contact->c_address->CurrentValue;
		$contact->c_address->CssStyle = "";
		$contact->c_address->CssClass = "";
		$contact->c_address->ViewCustomAttributes = "";

		// c_tel
		$contact->c_tel->ViewValue = $contact->c_tel->CurrentValue;
		$contact->c_tel->CssStyle = "";
		$contact->c_tel->CssClass = "";
		$contact->c_tel->ViewCustomAttributes = "";

		// c_subject
		$contact->c_subject->ViewValue = $contact->c_subject->CurrentValue;
		$contact->c_subject->CssStyle = "";
		$contact->c_subject->CssClass = "";
		$contact->c_subject->ViewCustomAttributes = "";

		// c_content
		$contact->c_content->ViewValue = $contact->c_content->CurrentValue;
		if (!is_null($contact->c_content->ViewValue)) $contact->c_content->ViewValue = str_replace("\n", "<br>", $contact->c_content->ViewValue); 
		$contact->c_content->CssStyle = "";
		$contact->c_content->CssClass = "";
		$contact->c_content->ViewCustomAttributes = "";

		// c_dateadd
		$contact->c_dateadd->ViewValue = $contact->c_dateadd->CurrentValue;
		$contact->c_dateadd->ViewValue = ew_FormatDateTime($contact->c_dateadd->ViewValue, 7);
		$contact->c_dateadd->CssStyle = "";
		$contact->c_dateadd->CssClass = "";
		$contact->c_dateadd->ViewCustomAttributes = "";

		// c_status
		if (strval($contact->c_status->CurrentValue) <> "") {
			switch ($contact->c_status->CurrentValue) {
				case "1":
					$contact->c_status->ViewValue = "Active";
					break;
				default:
					$contact->c_status->ViewValue = $contact->c_status->CurrentValue;
			}
		} else {
			$contact->c_status->ViewValue = NULL;
		}
		$contact->c_status->CssStyle = "";
		$contact->c_status->CssClass = "";
		$contact->c_status->ViewCustomAttributes = "";

		// c_id
		$contact->c_id->HrefValue = "";

		// c_fullname
		$contact->c_fullname->HrefValue = "";

		// c_email
		$contact->c_email->HrefValue = "";

		// c_address
		$contact->c_address->HrefValue = "";

		// c_tel
		$contact->c_tel->HrefValue = "";

		// c_subject
		$contact->c_subject->HrefValue = "";

		// c_content
		$contact->c_content->HrefValue = "";

		// c_dateadd
		$contact->c_dateadd->HrefValue = "";

		// c_status
		$contact->c_status->HrefValue = "";
	} elseif ($contact->RowType == EW_ROWTYPE_ADD) { // Add row
	} elseif ($contact->RowType == EW_ROWTYPE_EDIT) { // Edit row
	} elseif ($contact->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$contact->Row_Rendered();
}
?>
<?php

// Set up Starting Record parameters based on Pager Navigation
function SetUpStartRec() {
	global $nDisplayRecs, $nStartRec, $nTotalRecs, $nPageNo, $contact;
	if ($nDisplayRecs == 0) return;

	// Check for a START parameter
	if (@$_GET[EW_TABLE_START_REC] <> "") {
		$nStartRec = $_GET[EW_TABLE_START_REC];
		$contact->setStartRecordNumber($nStartRec);
	} elseif (@$_GET[EW_TABLE_PAGE_NO] <> "") {
		$nPageNo = $_GET[EW_TABLE_PAGE_NO];
		if (is_numeric($nPageNo)) {
			$nStartRec = ($nPageNo-1)*$nDisplayRecs+1;
			if ($nStartRec <= 0) {
				$nStartRec = 1;
			} elseif ($nStartRec >= intval(($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1) {
				$nStartRec = intval(($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1;
			}
			$contact->setStartRecordNumber($nStartRec);
		} else {
			$nStartRec = $contact->getStartRecordNumber();
		}
	} else {
		$nStartRec = $contact->getStartRecordNumber();
	}

	// Check if correct start record counter
	if (!is_numeric($nStartRec) || $nStartRec == "") { // Avoid invalid start record counter
		$nStartRec = 1; // Reset start record counter
		$contact->setStartRecordNumber($nStartRec);
	} elseif (intval($nStartRec) > intval($nTotalRecs)) { // Avoid starting record > total records
		$nStartRec = intval(($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1; // Point to last page first record
		$contact->setStartRecordNumber($nStartRec);
	} elseif (($nStartRec-1) % $nDisplayRecs <> 0) {
		$nStartRec = intval(($nStartRec-1)/$nDisplayRecs)*$nDisplayRecs+1; // Point to page boundary
		$contact->setStartRecordNumber($nStartRec);
	}
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
